package controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.print.attribute.standard.Compression;
import javax.sound.sampled.LineListener;
import javax.xml.crypto.Data;

import net.iharder.Base64;

public class BaseService_Picture {

	public static void string2pic(String picdata, String path) {
		try {
			// byte[] data = Base64.decode(umcompress(picdata.getBytes()));
			// //uncompress --> decode the picture data
			byte[] data = umcompress(Base64.decode(picdata.getBytes()));
			savefile(data, path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static String pic2string(String path) {
		// byte [] data = compress (Base64.encodeBytesToBytes(loadfile(path)) );
		// //loadfile --> encode --> compress
		byte[] data = Base64.encodeBytesToBytes(compress(loadfile(path)));
		return new String(data);
	}

	/**
	 * load imag ,then convert to byte[]
	 * 
	 * @param string(path)
	 * @return byte[](picdata)
	 */
	public static byte[] loadfile(String path) {
		byte[] data = null;
		byte[] buffer = null;
		FileInputStream fis = null;
		ByteArrayOutputStream bos = null;
		File file = new File(path);
		try {
			fis = new FileInputStream(file);
			bos = new ByteArrayOutputStream((int) file.length());
			buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			data = bos.toByteArray();
			bos.close();
			fis.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return data;
	}

	public static byte[] compress(byte[] encodeBytesToBytes) {
		// TODO Auto-generated method stub

		try {
			System.out.println("-----before cpmpress:" + encodeBytesToBytes.length);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gom = new GZIPOutputStream(bos);
			gom.write(encodeBytesToBytes);
			gom.flush();
			gom.close();
			byte[] pressdata = bos.toByteArray();
			bos.close();
			System.out.println("-----after cpmpress:" + pressdata.length);
			return pressdata;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * save the picdata into file
	 * 
	 * @param byte[]
	 *            (data)
	 * @param string
	 *            (path)
	 */

	public static void savefile(byte[] data, String path) {
		// TODO Auto-generated method stub

		try {
			FileOutputStream fos = new FileOutputStream(path);
			fos.write(data);
			fos.flush();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static byte[] umcompress(byte[] pressdata) {
		// TODO Auto-generated method stub
		byte[] unpressdata = null;
		ByteArrayInputStream bis = null;
		GZIPInputStream gim = null;
		ByteArrayOutputStream bos = null;
		try {
			bis = new ByteArrayInputStream(pressdata);
			gim = new GZIPInputStream(bis);
			bos = new ByteArrayOutputStream();
			int len = -1;
			byte[] buffer = new byte[1024];
			while ((len = gim.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			unpressdata = bos.toByteArray();
			bos.close();
			gim.close();
			bis.close();
			return unpressdata;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

}
